package com.hb.gp.util.command;

import java.util.List;

import com.hb.gp.util.MsgGenUtil;
import com.hb.gp.util.tools.DES;
import com.hb.gp.util.tools.GP_GetMac;

public class APDU_PutKey {
	public static String getPutKeyCommand(List<String> sessionKeys,
			List<String> newKeys, String p1, String lastICV, String keyIndex)
			throws Exception {
		String pbCardKeyEncSk = sessionKeys.get(0);
		String pbCardKeyMacSk = sessionKeys.get(1);
		String pbCardKeyDekSk = sessionKeys.get(2);
		if (pbCardKeyEncSk == null || pbCardKeyEncSk.equals("")) {
			throw new Exception("sessionEncKey is null ");
		}
		if (pbCardKeyMacSk == null || pbCardKeyMacSk.equals("")) {
			throw new Exception("sessionEncKeyMacKey is null ");
		}
		if (pbCardKeyDekSk == null || pbCardKeyDekSk.equals("")) {
			throw new Exception("sessionEncKeyDekKey is null ");
		}
		if (newKeys == null || newKeys.size() == 0) {
			throw new Exception("new key is empty!");
		}
		StringBuffer buf = new StringBuffer();
		String newKEncKey = null;
		String newKMacKey = null;
		String newKDekKey = null;
		buf.append("84D8");
		buf.append(p1);
		if (newKeys.size() > 1) {
			buf.append("81");
			newKEncKey = newKeys.get(0);
			newKMacKey = newKeys.get(1);
			newKDekKey = newKeys.get(2);
		} else if (newKeys.size() == 1) {
			buf.append("01");
			if (Integer.parseInt(keyIndex, 16) == 70) {
				String[] token = newKeys.get(0).split(",");
				buf.append("8970A180" + token[0] + "A003" + token[1] + "00");
				return buf.toString();
			}
			if (Integer.parseInt(keyIndex, 16) == 71) {
				newKEncKey = newKeys.get(0);
				if (newKEncKey != null) {
					keyIndex += "8110"
							+ cryptoData(pbCardKeyDekSk, newKEncKey).substring(
									0, 32) + "03" + getNewKeyValeu(newKEncKey);
					buf.append(MsgGenUtil.buf2hex(MsgGenUtil
							.Int2Bytes1(keyIndex.length() / 2)));
					buf.append(keyIndex);
					return buf.toString();
				}
			}
		}
//		if (Integer.parseInt(keyIndex, 16) < 10) {
			if (newKEncKey != null)
				keyIndex += "8010"
						+ cryptoData(pbCardKeyDekSk, newKEncKey).substring(0,
								32) + "03" + getNewKeyValeu(newKEncKey);
			if (newKMacKey != null)
				keyIndex += "8010"
						+ cryptoData(pbCardKeyDekSk, newKMacKey).substring(0,
							32) + "03" + getNewKeyValeu(newKMacKey);
//		} else {
//			if (newKEncKey != null)
//				keyIndex += "8110"
//						+ cryptoData(pbCardKeyDekSk, newKEncKey).substring(0,
//								32) + "03" + getNewKeyValeu(newKEncKey);
//			if (newKMacKey != null)
//				keyIndex += "8110"
//						+ cryptoData(pbCardKeyDekSk, newKMacKey).substring(0,
//								32) + "03" + getNewKeyValeu(newKMacKey);
//		}
		if (newKDekKey != null)
			keyIndex += "8010"
					+ cryptoData(pbCardKeyDekSk, newKDekKey).substring(0, 32)
					+ "03" + getNewKeyValeu(newKDekKey);
		buf.append(MsgGenUtil.buf2hex(MsgGenUtil.Int2Bytes1(keyIndex.length() / 2 + 8)));
		buf.append(keyIndex);
		String mac = GP_GetMac.getIMac(buf.toString(), pbCardKeyMacSk, lastICV,
				true);
		buf.append(mac);
		return buf.toString();
	}

	private static String cryptoData(String keyDekSk, String data)
			throws Exception {
		String retStr = new String();
		retStr = DES.encryptTDES_ECB(MsgGenUtil.BCD2Bytes(keyDekSk),
				MsgGenUtil.BCD2Bytes(data));
		return retStr;
	}

	private static String getNewKeyValeu(String newKey) {

		String retStr = "";
		String keyvalue = "";
		keyvalue = DES.encryptTDES_ECB(MsgGenUtil.BCD2Bytes(newKey),
				MsgGenUtil.BCD2Bytes("0000000000000000"));
		retStr += keyvalue.substring(0, 6);
		return retStr;
	}
}
